<template>
    <div class="cart-item">
        <div class="body row">
            <div class="info">
                <div class="title">{{ item.title }}</div>
                <div class="des">{{ item.des }}</div>
            </div>
            <div class="price row">
                <span>￥{{ item.pricePay }}</span>
            </div>
            <div class="actions row">
                <div
                    class="btn btn-del"
                    v-if="item.count > 0"
                    @click="updateCount(item.count - 1)"
                >
                    <img
                        src=""
                    />
                </div>
                <div class="count" v-if="item.count > 0">{{ item.count }}</div>
                <div class="btn btn-add" @click="updateCount(item.count + 1)">
                    <img
                        src="https://s3plus.meituan.net/v1/mss_e2821d7f0cfe4ac1bf9202ecf9590e67/cdn-prod/file:9096d347/40b1252b285172da0c8b7d88a2b46220.png"
                    />
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    props: {
        item: {
            type: Object,
            default() {
                return {
                    des: "少冰",
                    img: "",
                    price: "1600",
                    pricePay: "",
                    productId: "",
                    title: "古茗奶茶",
                    count: 0,
                    categoryId: "",
                    categoryName: ""
                };
            }
        }
    },
    data() {
        return {};
    },

    methods: {
        updateCount(newCount) {
            newCount = newCount < 0 ? 0 : newCount;
            const item = {
                ...this.item,
                categoryId: this.item.categoryId,
                categoryName: this.item.categoryName
            };
            if (newCount > this.item.count) {
                this.$store.commit("addCart", {
                    item
                });
            }
            if (newCount < this.item.count) {
                this.$store.commit("delCart", {
                    item
                });
            }
            console.log(this.$store.getters.cartMapList);
        }
    }
};
</script>


<style lang="less" scoped>
.cart-item {
    padding: 20px 30px;

    .body {
        justify-content: space-between;
        .info {
            flex: 1 1 auto;
            .title {
                font-weight: 600;
                margin-bottom: 10px;
            }
            .des {
                font-size: 24px;
            }
        }
        .price {
            align-items: center;
            padding: 0 50px 0 30px;
            text-align: right;
            color: #fb4e44;
            font-weight: 600;
            font-size: 32px;
        }
        .actions {
            align-items: center;
            .btn {
                width: 40px;
                height: 40px;
            }
            .count {
                height: 40px;
                line-height: 40px;
                padding: 0 15px;
            }
        }
    }
}
</style>